home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / LAN / TWAIT.ARJ / TAPEWAIT.C next >
C/C++ Source or Header  |  1992-06-24  |  6KB  |  239 lines

  1. /********************************************************
  2.  * TapeWait  -- Waits until only specified number of users are still
  3.  *              on the system or until the <Esc> or <A> key is pressed.
  4.  *
  5.  * Parameters    fileserver:    File Server to poll
  6.  *        maxusers:    Maximum number of users to ignore
  7.  *
  8.  * Returns    0:    Wrong Syntax
  9.  *        1:    <Esc> key pressed by user to proceed or automaic exit
  10.  *        2:    <A> key pressed to abort
  11.  ********************************************************/
  12.  
  13. #define        TIMEDELAY    10    /* Time delay in seconds */
  14.  
  15. #include    <conio.h>
  16. #include    <graph.h>
  17. #include    <stdio.h>
  18. #include    <stdlib.h>
  19. #include    <string.h>
  20. #include    <time.h>
  21. #include    <nit.h>
  22. #include    <niterror.h>
  23.  
  24. int    Pause(double);
  25. void    AtSay(short,short,char *),
  26.     ClearScreen(void),
  27.     ClearWindow(void),
  28.     DefineTextWindow(short,short,short,short),
  29.     ExitProgram(int),
  30.     HelpScreen(void),
  31.     SendBroadcast(void);
  32. WORD    DisplayUserList(void),
  33.     GetFileServerConnectionID(char *);
  34.  
  35. WORD    defaultConnectionID,
  36.     maxConnectionsSupported;
  37.  
  38. void    main(int argc, char *argv[])
  39. {
  40.     char        stringBuffer[81];
  41.     int        errorCode,exitCondition,keyStroke,maxNumberUsers;
  42.     WORD        fileServerNumber,numberUsers;
  43.  
  44.     FILE_SERV_INFO    serverInfo;
  45.  
  46.     /* Set variable defaults */
  47.     exitCondition=maxNumberUsers=0;
  48.  
  49.     /* Get the current connection number */
  50.     defaultConnectionID=GetPreferredConnectionID();
  51.  
  52.     /* If the number of parameters is 0 or help is asked, display help */
  53.     argc--;
  54.     if (!argc)
  55.         HelpScreen();
  56.     if ((argv[1][1]=='?') || (argv[1][1]=='h') || (argv[1][1]=='H'))
  57.         HelpScreen();
  58.  
  59.     /* Switch to the file server specified in argument 1 */
  60.     fileServerNumber=GetFileServerConnectionID(argv[1]);
  61.     if (!fileServerNumber)
  62.         HelpScreen();
  63.     SetPreferredConnectionID(fileServerNumber);
  64.     /* Disable broadcasts */
  65.     DisableBroadcasts();
  66.  
  67.     /* Get the maximum number of connections on the server */
  68.     errorCode=GetServerInformation(sizeof(serverInfo),&serverInfo);
  69.     if (errorCode)
  70.         ExitProgram(errorCode);
  71.     maxConnectionsSupported=serverInfo.maxConnectionsSupported;
  72.  
  73.     /* Get the maximum number of users, if not set, default to 1 */
  74.     if (argc > 1)
  75.         maxNumberUsers=atoi(argv[2]);
  76.     if (!maxNumberUsers)
  77.         maxNumberUsers=1;
  78.  
  79.     /* Draw the screen for displaying the users */
  80.     ClearScreen();
  81.     sprintf(stringBuffer,"TapeWait -- Waiting until only %d users left  (time delay %d seconds)",maxNumberUsers,(int)TIMEDELAY);
  82.     AtSay(1,1,stringBuffer);
  83.     AtSay(24,7,"Press <Esc> to Continue, <A> to Abort or <T> to re-send the Message");
  84.     AtSay(2,1,"Connection  User ID             Full Name");
  85.     AtSay(3,1,"----------  ------------------  ----------------------------------------");
  86.     DefineTextWindow(4,1,22,78);    
  87.  
  88.     /* Send the message to all users to logout */
  89.     SendBroadcast();
  90.  
  91.     while (!exitCondition)
  92.     {
  93.         /* Display the user list */
  94.         numberUsers=DisplayUserList();
  95.  
  96.         /* Check is number of users remaining is less that or equal to specified */
  97.         if (numberUsers <= (WORD)maxNumberUsers)
  98.         {
  99.             exitCondition=1;
  100.             break;
  101.         }
  102.  
  103.         /* Check for a keypress, if one, process */
  104.         if (Pause((double)TIMEDELAY))
  105.         {
  106.             keyStroke=getch();
  107.             switch (keyStroke)
  108.             {
  109.                 case    27:    exitCondition=1;    /* <Esc> */
  110.                         break;
  111.                 case    'A':
  112.                 case    'a':    exitCondition=2;    /* <A> */
  113.                         break;
  114.                 case    'T':    SendBroadcast();        /* <T> */
  115.                         break;
  116.             }
  117.         }
  118.     }
  119.     ExitProgram(exitCondition);
  120. }
  121.  
  122. void    AtSay(short row,short column,char *text)
  123. {
  124.     _settextposition(row,column);
  125.     _outtext(text);
  126. }
  127.  
  128. void    ClearScreen(void)
  129. {
  130.     _clearscreen(_GCLEARSCREEN);
  131. }
  132.  
  133. void    ClearWindow(void)
  134. {
  135.     _clearscreen(_GWINDOW);
  136. }
  137.  
  138. void    DefineTextWindow(short row1,short column1,short row2,short column2)
  139. {
  140.     _settextwindow(row1,column1,row2,column2);
  141. }
  142.  
  143. void    ExitProgram(int errorLevel)
  144. {
  145.     EnableBroadcasts();
  146.     SetPreferredConnectionID(defaultConnectionID);
  147.     exit(errorLevel);
  148. }
  149.  
  150. WORD    GetFileServerConnectionID(char *fileServer)
  151. {
  152.     WORD    i;
  153.     char    fileServerName[48];
  154.  
  155.     for (i=1; i <= 8; i++)
  156.         if (IsConnectionIDInUse(i))
  157.         {
  158.             GetFileServerName(i,fileServerName);
  159.             if (strcmpi(fileServer,fileServerName)==0)
  160.                 return(i);
  161.         }
  162.     return(0);
  163. }
  164.  
  165. void    HelpScreen(void)
  166. {
  167.     printf("TapeWait  --  Waits until only a specified number of users\n");
  168.     printf("              are logged on or until the <Esc> or <A> key is\n");
  169.     printf("              pressed.\n");
  170.     printf("Syntax: TapeWait <fileserver> <users remaining>\n");
  171.     printf("Return: 1 = Proceed with backup\n");
  172.     printf("        2 = Abort backup\n");
  173.     printf("        0 = Syntax error\n\n");
  174.     ExitProgram(0);
  175. }
  176.  
  177. int    Pause(double timeDelay)
  178. {
  179.     time_t    startTime,endTime;
  180.     int    keyPress;
  181.  
  182.     time(&startTime);
  183.     do
  184.     {
  185.         time(&endTime);
  186.         keyPress=kbhit();
  187.     } while ((difftime(endTime,startTime) < timeDelay) && !keyPress);
  188.     return(keyPress);
  189. }
  190.  
  191. void    SendBroadcast(void)
  192. {
  193.     BYTE    resultList[255];
  194.     WORD    connectionList[255];
  195.     WORD    connectionCount;
  196.  
  197.     for (connectionCount=0; connectionCount<maxConnectionsSupported; connectionCount++)
  198.         connectionList[connectionCount]=connectionCount;
  199.  
  200.     SendBroadcastMessage("Please LOG OUT, Backing up files.",connectionList,resultList,maxConnectionsSupported);
  201. }
  202.  
  203. WORD    DisplayUserList(void)
  204. {
  205.     BYTE    moreSegments,propertyFlags;
  206.     char    objectName[48],loginTime[8],propertyValue[128],stringBuffer[81];
  207.     int    loopCounter,objectType,errorCode;
  208.     long    objectID;
  209.     WORD    connectionNumber,usersLoggedOn;
  210.  
  211.     loopCounter=0;
  212.     usersLoggedOn=0;
  213.     connectionNumber=1;
  214.  
  215.     ClearWindow();
  216.     /* Display the user list in the defined window */
  217.     while ((loopCounter < 19) && (connectionNumber <= maxConnectionsSupported))
  218.     {
  219.         errorCode=GetConnectionInformation(connectionNumber,objectName,
  220.             &objectType,&objectID,loginTime);
  221.         if ((!errorCode) && (objectName[0] != '\0') && (objectType==OT_USER))
  222.         {
  223.             errorCode=ReadPropertyValue(objectName,objectType,"IDENTIFICATION",1,
  224.                 propertyValue,&moreSegments,&propertyFlags);
  225.  
  226.             if (!errorCode)
  227.                 sprintf(stringBuffer,"    %3d     %-18s  %-40s",connectionNumber,objectName,propertyValue);
  228.             else
  229.                 sprintf(stringBuffer,"    %3d     %-18s",connectionNumber,objectName);
  230.  
  231.             loopCounter++;
  232.             usersLoggedOn++;
  233.             AtSay((short)loopCounter,1,stringBuffer);
  234.         }
  235.         connectionNumber++;
  236.     }
  237.     return(usersLoggedOn);
  238. }
  239.